package com.winit.schedule.task.tracker.taskdeal;

import java.util.ArrayList;
import java.util.List;

import com.winit.schedule.core.model.Task;
import com.winit.schedule.core.task.ScheduleTaskDealMulti;
import com.winit.schedule.core.task.annotation.ScheduleTask;

@ScheduleTask(taskName = "demoTaskMulti")
public class OrderTaskDealMulti implements ScheduleTaskDealMulti<OrderInfo> {

    int count = 0;

    /**
     * 查询返回待处理的数据 task.getEachFetchDataNum()为控制台配置的每次批量查询数据的数量
     */
    @Override
    public List<OrderInfo> selectTasks(Task task) {

        // return null;
        count++;
        if (count % 3 == 0) {
            return null;
        }

        List<OrderInfo> orders = new ArrayList<OrderInfo>();
        for (int i = 0; i < task.getEachFetchDataNum(); i++) {

            orders.add(new OrderInfo("orderId-" + i * count, "orderName-" + i * count));
        }
        return orders;
    }

    /**
     * 会被多线程调用，通过队列的方式依次从查询到的数据中获取数据进行处理
     */
    @Override
    public boolean execute(OrderInfo[] t) {
        try {
            Thread.currentThread().sleep(500);
            // System.out.println(">>>>>>>>>>>>>>>>>" + t.getOrderId());
            if (null != t) {
                System.out.println(">>>>>"+Thread.currentThread().getName()+">>>>>>>>>>>batchSize: " + t.length);
            } else {
                System.out.println(">>>>>>>>>>>>>>>>>>>batchSize: null");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }

}
